﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
//---------------------
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNetNuke.Entities.Modules;
using System.Collections.Specialized;
using System.Web.UI.HtmlControls;
using System.IO;

namespace VluSDCEvent
{
    /// <summary>
    /// Summary description for VluNewsController
    /// </summary>
    public class Controller
    {
        #region Members *****************************************
        LinqDataContextDataContext oLinqData;
        int iModuleDefId = -1;
        #endregion Members --------------------------------------

        #region Properties *****************************************
        public LinqDataContextDataContext OLinqData
        {
            set { oLinqData = value; }
            get { return oLinqData; }
        }
        #endregion Properties --------------------------------------

        #region Methods *****************************************
        public Controller()
        {
            oLinqData = new LinqDataContextDataContext();
            //
            // TODO: Add constructor logic here
            //
        }

        public int MtdGetModuleDefId(int pModuleId, int pTabId)
        {
            if (iModuleDefId == -1)
            {
                ModuleController oModuleController = new ModuleController();
                ModuleInfo oModuleInfo = oModuleController.GetModule(pModuleId, pTabId);
                iModuleDefId = oModuleInfo.ModuleDefID;
            }
            return iModuleDefId;
        }

        public DateTime MtdGetDate()
        {
            return OLinqData.ExecuteQuery<DateTime>("SELECT GETDATE()").First();
        }

        /// <summary>
        /// * Using DotNetNuke.Services.Log.EventLog.ExceptionLogController to add an exception to log.
        /// </summary>
        /// <param name="e"></param>
        public static void MtdLogException(System.Exception e)
        {
            DotNetNuke.Services.Log.EventLog.ExceptionLogController oExceptionLogController = new DotNetNuke.Services.Log.EventLog.ExceptionLogController();
            oExceptionLogController.AddLog(e);
        }

        #region static *****************************************
        public static string MtdGetUrlLanguage(HttpRequest request, string sDefaultLanguage)
        {
            string sLanguage = MtdGetUrlParam(request, "language");
            if (string.IsNullOrEmpty(sLanguage))
                sLanguage = sDefaultLanguage;
            return sLanguage;
        }

        /// <summary>
        /// * Get paramete in the url address.
        /// </summary>
        /// <param name="paramName">+ Paramete name</param>
        /// <returns></returns>
        public static string MtdGetUrlParam(HttpRequest request, string sParamName)
        {
            return request.Params.Get(sParamName);
        }

        public static bool MtdSetUrlParam(HttpRequest request, string sParamName, string sParamValue)
        {
            if (!string.IsNullOrEmpty(sParamName))
            {
                if (request.Params.Get(sParamName) == null)
                    request.Params.Add(sParamName, sParamValue);
                else
                    request.Params.Set(sParamName, sParamValue);
                return true;
            }
            return false;
        }

        /// <summary>
        /// * Redirect to ctrNewsMaster control to load other control.
        /// </summary>
        /// <param name="response">+ HttpResponse object.</param>
        /// <param name="tabId">+ TabId of ModulePortalBase.</param>
        /// <param name="controlMain">+ Main control use to load other control. Alway have value="".</param>
        /// <param name="paramPair">+ Firt param name is "loading" which is control will be loaded into placeholder of ctrNewsMaster.</param>
        public static void MtdRedirect(HttpResponse response, int tabId, string mainControl, params string[] paramPair)
        {
            response.Redirect(DotNetNuke.Common.Globals.NavigateURL(tabId, mainControl, paramPair));
        }

        public static Control MtdLoadControl(PlaceHolder phd, Page page, string sModulePath, string sControlName, string sLanguage)
        {
            PortalModuleBase ctr = (PortalModuleBase)page.LoadControl(sModulePath + sControlName + ".ascx");
            ctr.ID = sControlName;
            // Set resource file.
            if (sLanguage.ToLower() == "en-us" || sLanguage.ToLower() == "en-us,en-us")
                ctr.LocalResourceFile += ".ascx.resx";
            else
                ctr.LocalResourceFile += sControlName + ".ascx." + sLanguage + ".resx";
            
            // Add control into placeholder.
            if (!phd.Controls.Contains(ctr))
                phd.Controls.Add(ctr);
            return ctr;
        }

        /// <summary>
        /// * Get Index of date format.
        /// * Return list of int have 3 element : firt is index of day, next is index of month, finish is index of year.
        /// </summary>
        /// <returns></returns>
        public static List<int> MtdGetIndexDateFormat()
        {
            List<int> lstIndex = new List<int>();
            string[] aDateFormat = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern.Split('/');
            IOrderedEnumerable<string> iOrderDate = aDateFormat.OrderBy(day => day);
            List<string> lstDateFormat = aDateFormat.AsEnumerable().ToList();
            lstIndex.Add(lstDateFormat.IndexOf(iOrderDate.ElementAt(0)));
            lstIndex.Add(lstDateFormat.IndexOf(iOrderDate.ElementAt(1)));
            lstIndex.Add(lstDateFormat.IndexOf(iOrderDate.ElementAt(2)));
            return lstIndex;
        }
        #endregion static --------------------------------------
        #endregion Methods --------------------------------------
    }
}